﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Versioning;

namespace Rio.Framework.Extensions
{
    public static class DocumentEx
    {

        /// <summary>
        /// Return the current document version
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static VersioningItem GetCurrentVersion(this Document p_oSource)
        {
            if (p_oSource.AllVersion != null)
            {
                foreach (var item in p_oSource.AllVersion)
                {
                    if (item.IsCurrentVersion)
                        return item;
                }
            }
            return null;
        }

        /// <summary>
        /// Return the number of major version in the document
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static int CurrentMajorCount(this Document p_oSource)
        {
            if (p_oSource.AllVersion != null)
            {
                return p_oSource.AllVersion.Count(i => i.Minor == 0);
            }
            return 0;
        }

        /// <summary>
        ///  Return the number of minor version in the document
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static int CurrentMinorCount(this Document p_oSource)
        {
            if (p_oSource.AllVersion != null)
            {
                int lastMajor = p_oSource.AllVersion.First().Major;
                return p_oSource.AllVersion.Where(i => i.Major == lastMajor && i.Minor > 0).Count();
            }
            return 0;
        }

        /// <summary>
        /// Indicate whether the max major version is reached
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static bool MaxMajorReached(this Document p_oSource)
        {
            if (p_oSource.AllVersion != null)
            {
                if (p_oSource.CurrentModelVersion.MaxMajorVersion <= p_oSource.CurrentMajorCount())
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Indicate whether the max minor version is reached
        /// </summary>
        /// <param name="p_oSource"></param>
        /// <returns></returns>
        public static bool MaxMinorReached(this Document p_oSource)
        {
            if (p_oSource.AllVersion != null)
            {
                if (p_oSource.CurrentModelVersion.MaxMinorVersion <= p_oSource.CurrentMinorCount())
                    return true;
            }
            return false;
        }


    }
}
